package com.hghivln.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.MedicinesMapper;
import com.hghivln.pojo.domain.Medicines;
import com.hghivln.pojo.dto.MedicinesDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.MedicinesService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@Service(methods = {@Method(name = "listMedicinesForPage",timeout = 10000)})
public class MedicinesServiceImpl implements MedicinesService {

    @Autowired
    private MedicinesMapper medicinesMapper;

    @Override
    public AjaxResult listMedicinesForPage(MedicinesDto medicinesDto) {
        Page<Medicines> page = new Page<>(medicinesDto.getPageNum(), medicinesDto.getPageSize());
        QueryWrapper<Medicines> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(medicinesDto.getMedicinesName()), Medicines.COL_MEDICINES_NAME, medicinesDto.getMedicinesName());
        qw.like(StringUtils.isNotBlank(medicinesDto.getKeywords()), Medicines.COL_KEYWORDS, medicinesDto.getKeywords());
        qw.eq(StringUtils.isNotBlank(medicinesDto.getMedicinesType()), Medicines.COL_MEDICINES_TYPE, medicinesDto.getMedicinesType());
        qw.eq(StringUtils.isNotBlank(medicinesDto.getProducterId()), Medicines.COL_PRODUCTER_ID, medicinesDto.getProducterId());
        qw.eq(StringUtils.isNotBlank(medicinesDto.getPrescriptionType()), Medicines.COL_PRESCRIPTION_TYPE, medicinesDto.getPrescriptionType());
        qw.eq(StringUtils.isNotBlank(medicinesDto.getStatus()), Medicines.COL_STATUS, medicinesDto.getStatus());
        qw.eq(Medicines.COL_DEL_FLAG, Constants.DEL_FALSE);
        medicinesMapper.selectPage(page, qw);
        System.out.println(page.getRecords());
        return AjaxResult.success("查询成功", page.getRecords(), page.getTotal());
    }

    @Override
    public AjaxResult addMedicines(MedicinesDto medicinesDto) {
        Medicines medicines = new Medicines();
        BeanUtils.copyProperties(medicinesDto, medicines);
        medicines.setCreateBy(medicinesDto.getSimpleUser().getUserName());
        medicines.setCreateTime(DateUtil.date());
        return AjaxResult.success(medicinesMapper.insert(medicines));
    }

    @Override
    public AjaxResult updateMedicines(MedicinesDto medicinesDto) {
        Medicines medicines = new Medicines();
        BeanUtils.copyProperties(medicinesDto, medicines);
        medicines.setUpdateBy(medicinesDto.getSimpleUser().getUserName());
        return AjaxResult.success(medicinesMapper.updateById(medicines));
    }

    @Override
    public AjaxResult deleteMedicinesByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        if (idList.size() > 0) {
            return AjaxResult.success(medicinesMapper.deleteMedicinesByIds(idList));
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult selectMedicinesById(Long id) {
        QueryWrapper<Medicines> qw = new QueryWrapper<>();
        qw.eq(Medicines.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.eq(Medicines.COL_MEDICINES_ID, id);
        return AjaxResult.success(medicinesMapper.selectOne(qw));
    }

    @Override
    public AjaxResult selectAllMedicines() {
        QueryWrapper<Medicines> qw = new QueryWrapper<>();
        qw.eq(Medicines.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.eq(Medicines.COL_STATUS, Constants.STATUS_TRUE);
        return AjaxResult.success(medicinesMapper.selectList(qw));
    }

    @Override
    public AjaxResult updateMedicinesStorage(Long medicinesId, Long medicinesStockNum) {
        return AjaxResult.success(medicinesMapper.updateMedicinesStorage(medicinesId, medicinesStockNum));
    }

}
